package com.brtbeacon.demo.lot.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.brtbeacon.demo.lot.R;
import com.brtbeacon.demo.lot.entity.FloorInfo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FloorAdapter extends RecyclerView.Adapter<FloorViewHolder> {

    private Context context;
    private List<FloorInfo> floorList = new LinkedList<>();

    private OnClickListener onClickListener;

    public FloorAdapter(Context context, List<FloorInfo> floorList) {
        this.context = context;
        if (floorList != null) {
            this.floorList.addAll(floorList);
        }
    }

    @NonNull
    @Override
    public FloorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        final FloorViewHolder holder = new FloorViewHolder(LayoutInflater.from(this.context).inflate(R.layout.bb_item_floor, viewGroup, false));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(holder);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull FloorViewHolder floorViewHolder, int i) {
        FloorInfo info = floorList.get(i);
        floorViewHolder.bind(info);
    }

    @Override
    public int getItemCount() {
        return floorList.size();
    }

    public void setFloors(List<FloorInfo> floorList) {
        this.floorList.clear();
        if (floorList != null) {
            this.floorList.addAll(floorList);
        }
        notifyDataSetChanged();
    }

    public List<FloorInfo> getFloors() {
        return new ArrayList<>(this.floorList);
    }

    public void selectFloor(int index) {

        if (index < 0 || index > floorList.size()) {
            return;
        }

        for (int floorIndex = 0; floorIndex < getItemCount(); ++floorIndex) {
            FloorInfo info = getFloors().get(floorIndex);
            info.setSelected(index == floorIndex);
        }

        notifyDataSetChanged();
    }

    public int getSelectIndex() {
        for (int index = 0; index < floorList.size(); ++index) {
            if (floorList.get(index).isSelected())
                return index;
        }

        return -1;
    }

    public void selectFloorByNumber(int floorNumber) {

        for (FloorInfo info: floorList) {
            if (info.getInfo().getFloorNumber() == floorNumber) {
                info.setSelected(true);
            } else {
                info.setSelected(false);
            }
        }

        notifyDataSetChanged();
    }

    public FloorInfo getSelectFloor() {
        for (FloorInfo info: floorList) {
            if (info.isSelected())
                return info;
        }
        return null;
    }

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick(FloorViewHolder holder);
    }

}
